from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse
from utils.tencent.sms import send_sms_single
from django.conf import settings
from web import models
from web.forms.project import ProjectModelForm 
from utils.tencent import cos

def show(request):
    # return HttpResponse('show')
    return render(request, 'web/project_show.html')

    
def project_list(request):
    if request.method == "GET":
        objs =  ProjectModelForm(request)
        # 获取项目，显示
        projects = {'my':[],'join':[],'star':[]}
        my_project_list = models.Project.objects.filter(creator = request.tracer.user)
        join_project_list =models.ProjectUser.objects.filter(user = request.tracer.user)
        projects_manage = {}
        projects_manage['my'] = my_project_list
        projects_manage['join'] = join_project_list
        for proj in my_project_list:
            if proj.star:
                item= {}  #封装star,type
                item['value']=proj
                item['type'] = 'my'
                projects['star'].append(item)
            else:
                projects['my'].append(proj)
        for proj in join_project_list:
            if proj.star:
                item= {}  #封装star,type
                item['value'] = proj.project
                item['type'] = 'join'
                projects['star'].append(item)
            else:
                projects['join'].append(proj.project)
        
        context = {'objs':objs,
                   'projects':projects,
                   'projects_manage':projects_manage} 
        return render(request,"web/project_list.html",context)
    else:
        objs = ProjectModelForm(request, data=request.POST)
        ret = {'status':True,'msg':''}
        if objs.is_valid():
            import time
            # 1. 新建cos buket并存储
            bucket = "{}-{}-{}".format(request.tracer.user.mobile_phone, 
                                    str(int(time.time()*1000)), "1253629186")
            region='ap-beijing'
            value = cos.create_bucket(bucket, region) 

            # 2. 新建项目
            objs.instance.bucket = bucket
            objs.instance.region = region 
            objs.instance.creator = request.tracer.user
            instance = objs.save()
            
            # 3. 初始化项目类型
            issues_type_obj_list = []
            for item in models.IssuesType.PROJECT_INIT_LIST:   #["任务", '功能', 'Bug']
                issues_type_obj_list.append(models.IssuesType(title = item, project=instance))
            
            models.IssuesType.objects.bulk_create(issues_type_obj_list)
            return JsonResponse(ret)
        else:
            ret['status'] = False
            ret['msg'] = objs.errors
            return JsonResponse(ret)
        
        
def project_star(request, project_type, project_id):
    if request.method == "GET":
        if project_type == 'my':
            models.Project.objects.filter(id = project_id).update(star=True)
        elif project_type =='join':
            models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=True)
        # elif project_type == 'star':
        #     # 我创建的项目？还是我参与的项目？
        #     existed = models.Project.objects.filter(id=project_id, creator=request.tracer.user).first()
        #     if existed:
        #         models.Project.objects.filter(id = project_id).update(star=False)
        #     else:
        #         models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=False)
        #     pass
    return redirect('/web/project/list/')

def project_unstar(request, project_type, project_id):
    if request.method == "GET":
        if project_type == 'my':
            models.Project.objects.filter(id = project_id).update(star=False)
        elif project_type =='join':
            models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=False)
    return redirect('/web/project/list/')

    